#include "stack.h"

void StackInit(Stack* ps)
{
	ps->a = NULL;
	ps->top = 0;
	ps->capacity = 0;
}

void StackDestory(Stack* ps)
{
	assert(ps);
	free(ps);
	ps = NULL;
}

void CheckCapacity(Stack* ps)
{
	if (ps->capacity == ps->top)
	{
		int newCapacity = ps->capacity == 0 ? 4 : 2 * ps->capacity;
		ps->a = (StackDataType*)realloc(ps->a, sizeof(Stack) * newCapacity);
		if (ps->a == NULL) {
			perror("realloc fail!\n");
			exit(-1);
		}
		else{
			ps->capacity = newCapacity;
		}
	}
}

void StackPush(Stack* ps, StackDataType x)
{
	assert(x);
	CheckCapacity(ps);
	ps->a[ps->top] = x;
	ps->top++;
}

void StackTop(Stack* ps)
{
	assert(ps);
	assert(ps->top != 0);
	printf("%d\n", ps->a[(ps->top) - 1]);
}

void StackPop(Stack* ps)
{
	assert(ps);
	assert(ps->top != 0);
	ps->top--;
}
